Consider an array of elements arr[5]= {5,4,3,2,1} , what are the steps...
Insertion Sort Steps for Array Arr[5] = {5, 4, 3, 2, 1}
Step 1: Compare the first two elements, i.e., 5 and 4. As 4 is smaller, swap them.
Arr[5] = {4, 5, 3, 2, 1}
Step 2: Compare the second and third elements, i.e., 5 and 3. As 3 is smaller, swap them.
Arr[5] = {4, 3, 5, 2, 1}
Step 3: Compare the third and fourth elements, i.e., 5 and 2. As 2 is smaller, swap them.
Arr[5] = {4, 3, 2, 5, 1}
Step 4: Compare the fourth and fifth elements, i.e., 5 and 1. As 1 is smaller, swap them.
Arr[5] = {4, 3, 2, 1, 5}
Step 5: Now, the first element is sorted. Compare the second and third elements, i.e., 3 and 2. As 2 is smaller, swap them.
Arr[5] = {4, 2, 3, 1, 5}
Step 6: Compare the first and second elements, i.e., 4 and 2. As 2 is smaller, swap them.
Arr[5] = {2, 4, 3, 1, 5}
Step 7: Compare the third and fourth elements, i.e., 3 and 1. As 1 is smaller, swap them.
Arr[5] = {2, 4, 1, 3, 5}
Step 8: Compare the second and third elements, i.e., 4 and 1. As 1 is smaller, swap them.
Arr[5] = {2, 1, 4, 3, 5}
Step 9: Compare the first and second elements, i.e., 2 and 1. As 1 is smaller, swap them.
Arr[5] = {1, 2, 4, 3, 5}
Step 10: Now, the first two elements are sorted. Compare the third and fourth elements, i.e., 4 and 3. As 3 is smaller, swap them.
Arr[5] = {1, 2, 3, 4, 5}
Step 11: Now, the first three elements are sorted. Compare the fourth and fifth elements, i.e., 4 and 5. As they are already in sorted order, no swapping is required.
Arr[5] = {1, 2, 3, 4, 5}
Final Arrangement: The final sorted array is {1, 2, 3, 4, 5}.